<template>
	<view class="border-top pt-3">
		<view class="px-2">
			<view class="item px-25">
				<view class="py-2 font30"><textarea value="" @blur="bindTextAreaBlur" style="width: 100%;height: 250rpx;" placeholder="输入问题" /></view>
				<view class="">
					<view class="font30 color333">上传照片</view>
					<view class="pt-2 pb-3 flex align-center">
						<view class="flex mr-3" v-if="file!=''">
							<image v-for="(item,index) in file" :key="index" :src="imgTip+item" style="width:120rpx;height:120rpx;margin-right: 10rpx;" mode=""></image>
						</view>
						<view v-if="imgNum!=0" @click="addimg" class="flex border align-center justify-center" style="width: 120rpx;height: 120rpx;">
							<image src="../../../../static/img/ic_add.png" style="width:120rpx;height:120rpx;" mode=""></image>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="px-2 py-2 font32 font-weight-500">请选择提问老师</view>
		<view class="px-2" style="margin-bottom: 130rpx;">
			<view class="flex align-center justify-between" v-for="(item, index) in list" :key="index" style="height: 100rpx;" @click="changeIndex(index)">
				<view class="flex align-center">
					<image
						src="../../../../static/img/ic_default_head.png"
						class="rounded-circle"
						style="width: 64rpx;height: 64rpx;"
						v-if="item.head_pic == 'http://39.97.172.251:9900'"
						mode=""
					></image>
					<image :src="item.head_pic" class="rounded-circle" style="width: 64rpx;height: 64rpx;" v-else mode=""></image>
					<view class="font30 color333 ml-3">{{ item.user_name }}</view>
				</view>
				<image src="../../../../static/img/ic_checked.png" style="width: 48rpx;height: 48rpx;" v-if="index == subindex" mode=""></image>
				<image src="../../../../static/img/ic_uncheck.png" style="width: 48rpx;height: 48rpx;" v-else mode=""></image>
			</view>
		</view>
		<view class="fixed-bottom px-5 mb-3" style="z-index: 999;"><main-button @click="toLeave">提交</main-button></view>
	</view>
</template>

<script>
import mainButton from '../../../../components/common/main-buttom.vue';
export default {
	components: { mainButton },
	data() {
		return {
			subindex: 0,
			list: [],
			longToId: '',
			file: [],
			vcContent: '',
			imgNum:1,
			imgTip:""
		};
	},
	onLoad() {
		this.getInfo();
	},
	methods: {
		changeIndex(index) {
			this.subindex = index;
			this.longToId = this.list[index].user_id;
		},
		addimg() {
			uni.chooseImage({
				count: this.imgNum,
				success: chooseImageRes => {
					const tempFilePaths = chooseImageRes.tempFilePaths;	
					console.log(tempFilePaths);
					this.imgNum=this.imgNum-tempFilePaths.length;
					for (var i = 0; i < tempFilePaths.length; i++) {
						uni.uploadFile({
							url: this.$http.baseUrl + '/FileUpload.shtml', //仅为示例，非真实的接口地址
							filePath: tempFilePaths[i],
							name: 'file',
							formData: {},
							success: uploadFileRes => {
								console.log(uploadFileRes);
								if (uploadFileRes.statusCode == 200) {
									let data = JSON.parse(uploadFileRes.data);
									this.imgTip=data.corePath;
									this.file.push(data.fileUrl)
								}
							}
						});
					};
					
				}
			});
		},
		bindTextAreaBlur: function(e) {
			console.log(e.detail.value);
			this.vcContent = e.detail.value;
		},
		toLeave() {
			let this_ = this;
			if (this_.vcContent != '') {
				let opts = {
					url: 'Topic/Save.shtml'
				};
				let param = {
					longToId: this_.longToId,
					longSponsor: uni.getStorageSync('userInfo').user_id,
					vcContent: this_.vcContent,
					vcTpdz: this_.file
				};
				this_.$http.httpRequest(opts, param).then(
					res => {
						console.log(res.data);
						if (res.data.code == '200') {
							uni.navigateBack({
								delta:1
							})
						} else {
							uni.showToast({
								icon: 'none',
								title: res.data.msg
							});
						}
					},
					error => {
						console.log(error);
					}
				);
			} else {
				uni.showToast({
					icon: 'none',
					title: '问题不可为空！'
				});
			}
		},
		getInfo() {
			let this_ = this;
			let opts = {
				url: 'SearchBj.shtml'
			};
			let param = {
				token: uni.getStorageSync('userInfo').token,
				class_code: uni.getStorageSync('classcode')
			};
			this_.$http.httpRequest(opts, param).then(
				res => {
					console.log(res.data);
					if (res.data.code == '200') {
						this_.list = res.data.data.teacher;
						this_.longToId = res.data.data.teacher[0].user_id;
					} else {
						uni.showToast({
							icon: 'none',
							title: res.data.msg
						});
					}
				},
				error => {
					console.log(error);
				}
			);
		}
	}
};
</script>

<style>
.item {
	background: rgba(255, 255, 255, 1);
	box-shadow: 0rpx 3rpx 10rpx 0rpx rgba(64, 64, 64, 0.12);
	border-radius: 20rpx;
}
</style>
